<?php

class ModuleController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		if(Yii::app()->user->isGuest)
		{
			$this->redirect($this->createUrl('login/index'));
		}		
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 */
	public function actionForSale()
	{
		$siteRoot = dirname(dirname(dirname(dirname(__FILE__))));

		if($_POST){
			$content = '<?php ';
			foreach($_POST['img'] as $key=>$val)
			{
                           $content .= '$moduleContend['.$key.'][\'img\'] = "'.$val.'";';
                           $content .= '$moduleContend['.$key.'][\'desc\'] = "'.$_POST['desc'][$key].'";';
						   $content .= '$moduleContend['.$key.'][\'url\'] = "'.$_POST['url'][$key].'";';

			}
			$content .= ' ?> ';
			file_put_contents($siteRoot.'/mytpl/module_for_sale_var.php', $content );
		}
		
		include $siteRoot.'/mytpl/module_for_sale_var.php';
		$this->render('view', array('module_content'=>$moduleContend));
	}


	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionUpdate()
	{
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$this->actionForSale();
	}


}
